package com.example.utils;

import com.example.business.entity.SysUser;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author ZhangPeng
 * @Description //todo
 * @date
 * @updateBy
 * @other
 **/

public class UserUtil {


    private static Logger logger = LoggerFactory.getLogger(UserUtil.class);


    @Autowired
    private RedisUtil redisUtil;



    public void clearCache() {
        SysUser sysUser = UserUtil.getUser();
        redisUtil.del("sysUser"+sysUser.getId());
    }

    public  void updateCache(SysUser user) {
//        RedisUtil.set("userCache", "id_" + user.getId());
//        RedisUtil.set("userCache", "ln" + user.getLoginName());
    }

    /**
     * @param
     * @return java.lang.Object
     * @Description //todo 获取公共登录人对象
     * @author ZhangPeng
     * @date 2020/7/2 13:53
     * @other
     **/
    public static Object principal() {
        Object principal = null;
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()) {
            principal = subject.getPrincipals().getPrimaryPrincipal();
        }
        return principal;
    }

    /**
     * @param
     * @return com.ssr.master.modules.entity.SysUser
     * @Description //todo 获取登录人信息
     * @author ZhangPeng
     * @date 2020/7/2 13:49
     * @other
     **/
    public static SysUser getUser() {
        if (principal() instanceof SysUser) {
            return (SysUser) principal();
        }
        return new SysUser();
    }


//    public static Principal getPrincipal() {
//        try {
//            Subject subject = SecurityUtils.getSubject();
//            Principal principal = (Principal)subject.getPrincipal();
//            if (principal != null) {
//                return principal;
//            }
//        } catch (UnavailableSecurityManagerException var2) {
//            ;
//        } catch (InvalidSessionException var3) {
//            ;
//        }
//
//        return null;
//    }


}
